package com.cloudsigns.factorymanager2.produce.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetAccountListModel implements IGetAccountListModel {

    @Override
    public void getAccountList(final OnGetAccountListListener onGetAccountListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(ProjectUtil.GET_ACCOUNT_LIST);
                HttpResponse response;
                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    Log.e("GetAccountListModel", "ProjectUtil=" + ProjectUtil.GET_ACCOUNT_LIST);
                    Log.e("GetAccountListModel", "result=" + result);
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onGetAccountListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("accountItemList"));
                            List<AccountInfoBean> accountItemList = new ArrayList<>();
                            if (jsonArr != null) {
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        AccountInfoBean accountInfoBean = new AccountInfoBean();
                                        accountInfoBean.setId(jsonObj.getString("id"));
                                        accountInfoBean.setAccountName(jsonObj.getString("name"));
                                        accountInfoBean.setAvatar(jsonObj.getString("avatar"));
                                        accountItemList.add(accountInfoBean);
                                    }
                                }
                                onGetAccountListListener.successed(accountItemList);
                            }
                        } else {
                            onGetAccountListListener.failed(code + "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnGetAccountListListener {

        void successed(List<AccountInfoBean> accountItemList);

        void failed(String desc);
    }
}
